package com.liyong.bos.controller;

import com.liyong.bos.domain.TUser;
import com.liyong.bos.domain.WeatherInfo;
import com.liyong.bos.service.GetWeatherService;
import com.liyong.bos.service.StaffService;
import com.liyong.bos.utils.JsonResult;
import com.liyong.bos.utils.UUIDUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * Created by ApacheMoy on 2017/4/22.
 */
@Controller
@RequestMapping("/mobileUser")
public class MobileGroupController {
    @Autowired
    private StaffService staffService;
    @Autowired
    private GetWeatherService getWeatherService;
    @ResponseBody
    @RequestMapping(value = "/checkLogin")
    public JsonResult mobileUserLogin(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("++"+username+"   +"+password);
        TUser tUser=staffService.finUser(username,password);
        JsonResult jsonResult=new JsonResult();
        if(tUser!=null){
            jsonResult.setMsg("ok");
            jsonResult.setData(tUser.getUsername());
        }else {
            jsonResult.setMsg("error");
        }
        return  jsonResult;
    }


    @ResponseBody
    @RequestMapping(value = "/mobileUserReg")
    public JsonResult mobileUserReg(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        TUser tUser=new TUser();
        tUser.setUsername(username);
        tUser.setPassword(password);
        tUser.setId(UUIDUtil.getUUID());
        JsonResult jsonResult=new JsonResult();
        try {
            staffService.insertUser(tUser);
        }catch (Exception e){
            jsonResult.setMsg("error");
            return jsonResult;
        }
        jsonResult.setMsg("ok");
        return  jsonResult;
    }

    @RequestMapping(value = "/weatherInfo")
    public @ResponseBody
    WeatherInfo getMobileWeatherData(HttpServletRequest request, HttpServletResponse response) {
        response.setCharacterEncoding("UTF-8");
        //String cityName=request.getParameter("cityName");
        String cityName = null;
        try {
            cityName = new String(request.getParameter("cityName").getBytes("ISO-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        WeatherInfo weatherInfo = new WeatherInfo();
        if (StringUtils.isNotBlank(cityName)) {
            weatherInfo = getWeatherService.getWeatherByCityName(cityName);
        } else {
            weatherInfo = getWeatherService.getWeatherByCityName("长沙");
        }
        return weatherInfo;
    }
}
